package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 旅游地区资源控制器
 */
@RestController
@RequestMapping("/regions")
public class RegionsController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    ServletContext servletContext;//应用上下文对象
    /**
     * 获取所有旅游地区的接口
     * @return
     */
    @GetMapping
    public List<Region> place(Long state){
        List<Region> regions = regionService.listPlaces(state);
        return regions;
    }
    /**
     * 获取某个地区下状态为推荐的攻略
     * @param qo
     * @return
     */
    @GetMapping(value = "/{placeId}/strategies",produces = "text/html;charset=UTF-8")
    public void selectQueryByPlaceId(StrategyQueryObject qo, HttpServletResponse response) throws Exception {
        qo.setState(Strategy.STATE_HOT);//设置攻略状态推荐
        qo.setPageSize(0);
        PageInfo hot = strategyService.query(qo);
        //查询此地区下所有的攻略
        qo.setState(null);
        qo.setPageSize(4);
        PageInfo all = strategyService.query(qo);

        //创建FreeMark配置对象
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        //配置模板的目录
        configuration.setDirectoryForTemplateLoading(new File(servletContext.getRealPath("template")));
        //获取模板
        configuration.setDefaultEncoding("UTF-8");
        Template template = configuration.getTemplate("strategyTemplate.ftl");
        //合成数据
        HashMap<String,Object> map = new HashMap<>();
        map.put("hot",hot);
        map.put("all",all);
        //设置响应类型和编码
        response.setContentType("text/html;charset=UTF-8");
        template.process(map,response.getWriter());
    }
    /**
     * 获取某个地区下状态为推荐的攻略
     * @param qo
     * @return
     */
    @GetMapping(value = "/{placeId}/strategies",produces = "application/json;charset=UTF-8")
    public PageInfo selectQueryByPlaceId(StrategyQueryObject qo) throws Exception {
        //查询此地区下所有的攻略
        qo.setState(null);
        qo.setPageSize(4);
        return strategyService.query(qo);
    }
}
